package com.holyant.goods.dto;

import com.alibaba.fastjson.JSONArray;
import com.ruoyi.common.core.annotation.Excel;
import com.ruoyi.common.core.annotation.NonNullVerification;
import com.ruoyi.common.core.constant.RegexpConstants;
import com.ruoyi.common.utils.LocalDateTimeUtils;
import lombok.Data;
import org.apache.commons.lang3.StringUtils;

import javax.validation.constraints.NotEmpty;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 商品导入dto
 *
 * @author zgx
 * @Title:
 * @Package
 * @Description:
 * @date 2022/5/5 14:17
 */
@Data
public class GdsGoodsImportDto {

    private static final String DATE_TIME_SUFFIX="-01";


//    @Pattern(regexp = "^[1-9]\\d*$", message = "商品条码仅支持填入数字！")
    @Excel(name = "*商品条码")
    private String barcode;

    /**
     * 商品中文名
     */
    @NotEmpty(message = "商品中文名不能为空！")
    @Excel(name = "*商品中文名")
    private String name;

    /**
     * 商品英文名称
     */
    @Excel(name = "商品英文名")
    private String eName;

    private Long categoryId;
    @NotEmpty(message = "商品一级分类不能为空！")
    @Excel(name = "*商品一级分类",prompt = "请填写ERP商品分类一级分类名称")
    private String categoryName;
    private String categoryCode;


    @Excel(name = "商品二级分类",prompt = "请填写ERP商品分类二级分类名称")
    private String goodsSecondaryClassification;

    @Excel(name = "商品三级分类",prompt = "请填写ERP商品分类三级分类名称")
    private String goodsLevel3Classification;


    private Long taxCategoryId;
    private String taxCategoryCode;
    @NotEmpty(message = "商品税率一级分类不能为空！")
    @Excel(name = "*税率一级分类",prompt = "请填写ERP商品税率分类一级分类名称")
    private String taxCategoryBane;


    @NotEmpty(message = "商品税率二级分类不能为空！")
    @Excel(name = "*税率二级分类",prompt = "请填写ERP商品税率分类二级分类名称")
    private String taxRateSecondaryClassification;

    private Long busCategoryId;
    @NotEmpty(message = "经营类目不能为空！")
    @Excel(name = "*经营类目",prompt = "请填写ERP经营类目中的类目名称")
    private String busCategoryName;

    private Long brandId;
    @NotEmpty(message = "商品品牌不能为空！")
    @Excel(name = "*品牌",prompt = "请填写ERP商品品牌中的品牌中文名")
    private String brandName;

    private Long unitId;
    @NotEmpty(message = "商品销售单位不能为空！")
    @Excel(name = "*销售单位",prompt = "请填写袋、罐、千克、克、公斤、吨、瓶、盒、包、KG、箱、组、支、毫克")
    private String unitName;


    @NotEmpty(message = "商品商品规格不能为空！")
    @Excel(name = "*商品规格",prompt = "请填写大于0的数")
    private String specsQty;

    @NotEmpty(message = "商品规格单位不能为空！")
    @Excel(name = "*规格单位",prompt = "请填写毫升、克、片、瓶、袋、毫克")
    private String specsUnit;


    @NonNullVerification(message = "商品重量(g)错误！",regexp = RegexpConstants.POSITIVE_INTEGER)
    @Excel(name = "重量(g)",prompt = "请填写大于0的数")
    private String netWeight;

    /**
     * 长
     */
    @NonNullVerification(message = "商品长(cm)错误！",regexp = RegexpConstants.POSITIVE_INTEGER)
    @Excel(name = "长(cm)",prompt = "请填写大于0的数")
    private String volumeLong;
    /**
     * 宽
     */
    @NonNullVerification(message = "商品请填写正确的商品规格",regexp = RegexpConstants.POSITIVE_INTEGER)
    @Excel(name = "宽(cm)",prompt = "请填写大于0的数")
    private String volumeWide;
    /**
     * 高
     */
    @NonNullVerification(message = "商品重量(g)错误！",regexp = RegexpConstants.POSITIVE_INTEGER)
    @Excel(name = "高(cm)",prompt = "请填写大于0的数")
    private String volumeHigh;

    @NotEmpty(message = "商品原产国不能为空！")
    @Excel(name = "*原产国")
    private String originCountry;

    @NotEmpty(message = "商品保质期不能为空！")
    @Excel(name = "*保质期(月)",prompt = "请填写大于0的整数")
    private String qualityPeriod;

    /**
     * 最小单位
     */
    @NotEmpty(message = "商品辅助单位(最小)不能为空！")
    @Excel(name = "*辅助单位(最小)",prompt = "请填写箱、套、组、拖、罐、袋、包、瓶、盒、支")
    private String minUnit;
    private String unitCode;

    @NotEmpty(message = "商品单位转换(最小)不能为空！")
    @Excel(name = "*单位转换(最小)",prompt = "请依据格式:X=Y，X辅助单位=Y销售单位，X、Y为大于0的整数")
    private String unitConversion;

    @Excel(name = "辅助单位",prompt = "请填写箱、套、组、拖、罐、袋、包、瓶、盒、支")
    private String assistUnit;

    private String assistUnitCode;

    @Excel(name = "单位转换",prompt = "请依据格式:X=Y，X辅助单位=Y销售单位，X、Y为大于0的整数")
    private String assistUnitConversion;


    @NotEmpty(message = "商品效期1不能为空！")
    @Excel(name = "*效期1",prompt = "请依据格式:yyyy-mm，例如2022-04")
    private String effective1;

    @NonNullVerification(message = "商品效期2错误！",regexp = RegexpConstants.YEAR_MONTH)
    @Excel(name = "效期2",prompt = "请依据格式:yyyy-mm，例如2022-04")
    private String effective2;

    @NonNullVerification(message = "商品效期3错误！",regexp = RegexpConstants.YEAR_MONTH)
    @Excel(name = "效期3",prompt = "请依据格式:yyyy-mm，例如2022-04")
    private String effective3;

    @NonNullVerification(message = "商品效期4错误！",regexp = RegexpConstants.YEAR_MONTH)
    @Excel(name = "效期4",prompt = "请依据格式:yyyy-mm，例如2022-04")
    private String effective4;

    @NonNullVerification(message = "商品效期5错误！",regexp = RegexpConstants.YEAR_MONTH)
    @Excel(name = "效期5",prompt = "请依据格式:yyyy-mm，例如2022-04")
    private String effective5;


    /**
     * @return 返回全部效期
     */
    public List<String> getEffectives() {
        List<String> lists = new ArrayList<>(5);
        lists.add(effective1);
        lists.add(effective2);
        lists.add(effective3);
        lists.add(effective4);
        lists.add(effective5);
        return lists.stream().filter(StringUtils::isNotEmpty).map(item->item.concat(DATE_TIME_SUFFIX)).collect(Collectors.toList());
    }

    public List<GdsSkuSaveDto> getGdsSkuSaveDtoList() {
        List<GdsSkuSaveDto> gdsSkuSaveDtoList = new ArrayList<>();
        GdsSkuSaveDto gdsSkuSaveDto = null;
        for (String effective : getEffectives()) {
            gdsSkuSaveDto = new GdsSkuSaveDto();
            gdsSkuSaveDto.setName(name);
            gdsSkuSaveDto.setExpireDate(LocalDateTimeUtils.parseLocalDate(effective, LocalDateTimeUtils.PATTERN_YYYY_MM_DD));
            gdsSkuSaveDto.setStatus("0");
            gdsSkuSaveDto.setExtAttr(new JSONArray().toJSONString());
            gdsSkuSaveDtoList.add(gdsSkuSaveDto);
        }
        return gdsSkuSaveDtoList;
    }

    public List<GdsGoodsSaveDto.AssistUnit> buildAssistUnitList(){
        // 最小单位-minUnit
        // 单位转换-unitConversion 1=2
        List<GdsGoodsSaveDto.AssistUnit> assistUnitList=new ArrayList<>();
        GdsGoodsSaveDto.AssistUnit minAssistUnit = new GdsGoodsSaveDto.AssistUnit();
        minAssistUnit.setMinUnit(1);
        minAssistUnit.setUnitCode(unitCode);
        minAssistUnit.setUnitName(minUnit);
        minAssistUnit.setCurQty(Integer.valueOf(unitConversion.split("=")[0]));
        minAssistUnit.setCastQty(Integer.valueOf(unitConversion.split("=")[1]));
        assistUnitList.add(minAssistUnit);
        if (StringUtils.isNotBlank(assistUnitConversion)) {
            GdsGoodsSaveDto.AssistUnit assistUnit = new GdsGoodsSaveDto.AssistUnit();
            assistUnit.setMinUnit(0);
            assistUnit.setUnitCode(this.assistUnitCode);
            assistUnit.setUnitName(this.assistUnit);
            assistUnit.setCurQty(Integer.valueOf(assistUnitConversion.split("=")[0]));
            assistUnit.setCastQty(Integer.valueOf(assistUnitConversion.split("=")[1]));
            assistUnitList.add(assistUnit);
        }
        return assistUnitList;
    }


    /**
     * @return 返回三级分类集合
     */
    public List<String> getCategoryNames(){
        List<String> categorys = new ArrayList<>(3);
        categorys.add(categoryName);
        categorys.add(goodsSecondaryClassification);
        categorys.add(goodsLevel3Classification);
        return categorys.stream().filter(StringUtils::isNotEmpty).collect(Collectors.toList());
    }


    /**
     * @return *税率二级分类
     */
    public List<String> getTaxCategoryBanes(){
        List<String> taxCategoryBanes = new ArrayList<>(2);
        taxCategoryBanes.add(taxCategoryBane);
        taxCategoryBanes.add(taxRateSecondaryClassification);
        return taxCategoryBanes.stream().filter(StringUtils::isNotEmpty).collect(Collectors.toList());
    }
}
